<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="园所名称" prop="title">
        <el-input v-model="queryParams.title" @input="handleInput" placeholder="请输入园所名称" clearable size="small"
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['student:studentEnroll:add']">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="studentEnrollList">
      <el-table-column type="index" label="序号" align="center" :index="customeIndex" width="80"></el-table-column>
      <!-- <el-table-column type="selection" width="55" align="center" /> -->
      <el-table-column label="园所名称" align="center" prop="schoolName" />
      <!-- <el-table-column label="年级" align="center" prop="specName" />
        <el-table-column label="班级" align="center" prop="specName" /> -->
      <el-table-column label="学期" align="center" prop="schoolTerm" />
      <el-table-column label="学位数" align="center" prop="spaceNumber" />
      <!-- <el-table-column label="实招人数" align="center" prop="realityNumber" /> -->
      <!-- <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['student:studentEnroll:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['student:studentEnroll:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改招生计划对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="园所名称" prop="name">
          <el-select v-model="form.name" clearable filterable placeholder="请选择园所名称">
            <el-option v-for="item in schoolList" :key="item.code" :label="item.title" :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="学期" prop="year">
          <el-date-picker style="margin-right: 20px;" v-model="form.year" type="year" value-format="yyyy" :editable="false"
            placeholder="请选择年">
          </el-date-picker>
          <el-radio-group v-model="form.xq">
            <el-radio label="上">上</el-radio>
            <el-radio label="下">下</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="招生人数" prop="total" >
          <el-input v-model="form.total" type='number' placeholder="请输入招生人数" style="width: 220px;"/>
        </el-form-item>
        <!-- <el-form-item label="报名人数" prop="signups" v-if="form.id">
            <el-input v-model="form.signups" disabled placeholder="请输入报名人数" />
          </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
<script>
import { getToken } from "@/utils/auth";
import { enrollPlanList, addenrollPlan, updateenrollPlan, delenrollPlan } from "@/api/qu/xwgl.js";
import { schoolList } from "@/api/qu/yssctj";
import moment from "moment";
export default {
  name: "StudentEnroll",
  data() {
    return {
      schoolList: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      id: '',
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 招生计划表格数据
      studentEnrollList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 状态字典
      statusOptions: [],
      // 导入参数
      upload: {
        // 是否显示弹出层
        open: false,
        // 弹出层标题
        title: "",
        // 是否禁用上传
        isUploading: false,
        // 是否更新已经存在的数据
        updateSupport: 0,
        // 设置上传的请求头部
        headers: { Authorization: "Bearer " + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + "/student/studentEnroll/importData"
      },
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        specId: null,
        period: null,
      },
      // 表单参数
      form: {
        name: '',
        semester: '',
        xq: '上',
        year: ''
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "标题不能为空", trigger: "blur" }
        ],
        year: [
          { required: true, message: "学期不能为空", trigger: "blur" }
        ],
        total: [
          { required: true, message: "招生人数不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getschoolList()
  },
  methods: {
    /** 查询学位管理列表 */
    async getList() {
      this.loading = true;
      try {
        const obj = {
          schoolName: this.queryParams.title,
          pageNum: this.queryParams.pageNum,
          pageSize: this.queryParams.pageSize,

        }
        const data = await enrollPlanList(obj);
        this.studentEnrollList = data.rows;
        this.total = data.total;
        this.loading = false;
      } catch (error) {
        console.log(error);
      }
    },
    // 状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    //获取学校列表
    async getschoolList() {
      const data = await schoolList();
      this.schoolList = data.rows;

    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    handleInput(val) {
      let regtszf = /[`~!@#$%^&*()_\-+=<>?:"{}|,./;'\\[\]·~！@#￥%……&*（）——\-+={}|《》？：“”【】、；‘'，。、]/g // 特殊字符正则
      let regkg = /[ ]/g// 空格正则
      this.queryParams.title = val.replace(regtszf, '').replace(regkg, '')
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        total: null,
        signups: null,
        content: null,
        name: '',
        year: '',
        xq: '上',
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 序号
    customeIndex(index) {
      return index + 1 + (this.queryParams.pageNum - 1) * this.queryParams.pageSize
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.id = ''
      this.reset();
      this.open = true;
      this.title = "新增招生学位数";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.$nextTick(() => {
        this.$set(this.form, 'name', row.schoolCode)
      })
      this.id = row.id;
      this.form.total = row.spaceNumber;
      this.form.year = row.schoolTerm.slice(0,4)
      const state = row.schoolTerm.slice(4,5)
      if(state === '上'){
        this.form.xq = '上'
      }else{
        this.form.xq = '下'
      }
      this.open = true;
      this.title = "修改招生学位数";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(async valid => {
        if (valid) {
          if (this.id != '') {
            try {
              const obj = {
                id: this.id,
                schoolCode: this.form.name,
                spaceNumber: this.form.total,
                schoolTerm: this.form.year + this.form.xq
              }
              const data = await updateenrollPlan(obj)
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            } catch (error) {
              console.log(error);
            }
          } else {
            try {
              const obj = {
                schoolCode: this.form.name,
                spaceNumber: this.form.total,
                schoolTerm: this.form.year + this.form.xq
              }
              const data = await addenrollPlan(obj);
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            } catch (error) {
              console.log(error);
            }
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id;
      this.$confirm('是否确认删除该学位数?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        return delenrollPlan({ id: ids });
      }).then(() => {
        this.getList();
        this.msgSuccess("删除成功");
      })
    },
  }
};
</script>
<style  lang="scss" scoped>
::v-deep input::-webkit-inner-spin-button {
  -webkit-appearance: none !important;
}

::v-deep input[type='number'] {
  -moz-appearance: textfield !important;
}
</style>